/*
 * Created by JFormDesigner on Mon Aug 28 22:52:13 CST 2023
 */

package src;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class changePage extends JFrame {
    public static StudentData sd = new StudentData();

    public static Student st = new Student();
    public int index = -1;
    public changePage() {
        initComponents();
    }

    private void button2MouseClicked(MouseEvent e) {
        this.dispose();
        new mainPage().setVisible(true);
    }

    public boolean textCheck(){
        boolean flag = true;
        String mcname = "^[\u4e00-\u9fa5]{2,10}$";
        String mcgrade = "\\d{4}$";
        String mcborn = "\\d{4}-\\d{2}-\\d{2}";
        if(!this.textFieldName.getText().matches(mcname)){
            flag =false;
            this.labelError.setText("姓名有误！(姓名应为纯中文！长度限制2-10)");
        }
        else if(!this.radioButtonMan.isSelected() && !this.radioButtonWomen.isSelected()) {
            flag = false;
            this.labelError.setText("请选择性别！");
        }
        else if(!this.textFieldBorn.getText().matches(mcborn)){
            flag = false;
            this.labelError.setText("请检查出生日期！格式（yyyy-mm-dd）");
        }

        return flag;
    }
    public void clean(){
        this.textFieldNewnumber.setText("");
        this.textFieldName.setText("");
        this.textFieldPro.setText("");
        this.textFieldBorn.setText("");
        this.radioButtonMan.setSelected(false);
        this.radioButtonWomen.setSelected(false);

    }

    private void buttonFindMouseClicked(MouseEvent e) throws IOException {
        this.labelError.setText("");
        sd.getFileDate();
        int i = sd.findData(sd.stdsdata, sd.capacity,this.textFieldNumber.getText());
        if(i == -1){
            clean();
            this.labelError.setText("查无此人，请检查学号输入是否正确！");

        }else{
            this.textFieldName.setText(sd.stdsdata[i].getName());
            String sex = sd.stdsdata[i].getSex();
            if(sex.equals("男")){
                this.radioButtonMan.setSelected(true);
                this.radioButtonWomen.setSelected(false);
            }
            if(sex.equals("女")){
                this.radioButtonMan.setSelected(false);
                this.radioButtonWomen.setSelected(true);

            }
            this.textFieldPro.setText(sd.stdsdata[i].getPro());
            this.textFieldBorn.setText(sd.stdsdata[i].getBorn());
            //信息映射到各个框
            this.buttonChange.setEnabled(true);
            this.buttonInit.setEnabled(true);
            //启用确认和信息重置键
            index = i;
            st = sd.stdsdata[i];
        }
    }
    

    private void Buttonchange(ActionEvent e) throws IOException {
        boolean flag = true;
        String sex = "";
            if (this.radioButtonMan.isSelected() && !this.radioButtonWomen.isSelected()) sex = "男";
            if (!this.radioButtonMan.isSelected() && this.radioButtonWomen.isSelected()) sex = "女";

        String newnumber = this.textFieldNewnumber.getText();
        int tempindex = sd.findData(sd.stdsdata,sd.capacity,newnumber);
        if(tempindex != -1 && tempindex != index){
            flag = false;
            this.labelError.setText("学号与"+sd.stdsdata[tempindex].getName()+"同学 学号("+sd.stdsdata[tempindex].getNumber()+")重复,请重试！");
        }
        
        
        if(flag && textCheck()){
            sd.stdsdata[index].setData(newnumber,this.textFieldName.getText(),sex,this.textFieldPro.getText(),sd.stdsdata[index].getGrade(),this.textFieldBorn.getText());
            sd.setAllStudents(sd.stdsdata,sd.capacity);
            this.labelError.setText("信息修改成功！");
            st = null;//释放缓存学生数据

        }
    }

    private void ButtonTextinit(ActionEvent e) {
        this.textFieldName.setText(st.getName());
        String sex = st.getSex();
        if(sex.equals("男")){
            this.radioButtonMan.setSelected(true);
            this.radioButtonWomen.setSelected(false);
        }
        if(sex.equals("女")){
            this.radioButtonMan.setSelected(false);
            this.radioButtonWomen.setSelected(true);

        }
        this.textFieldPro.setText(st.getPro());
        this.textFieldBorn.setText(st.getBorn());
        labelError.setText("已还原学生信息");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        label1 = new JLabel();
        textFieldNumber = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        textFieldName = new JTextField();
        textFieldPro = new JTextField();
        textFieldBorn = new JTextField();
        radioButtonMan = new JRadioButton();
        radioButtonWomen = new JRadioButton();
        buttonFind = new JButton();
        buttonChange = new JButton();
        buttonBack = new JButton();
        buttonInit = new JButton();
        labelError = new JLabel();
        label6 = new JLabel();
        textFieldNewnumber = new JTextField();

        //======== this ========
        setTitle("\u4fee\u6539\u7a97\u53e3");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u8bf7\u8f93\u5165\u9700\u8981\u4fee\u6539\u5b66\u751f\u7684\u5b66\u53f7\uff1a");
            panel1.add(label1);
            label1.setBounds(10, 20, 170, 25);
            panel1.add(textFieldNumber);
            textFieldNumber.setBounds(190, 20, 175, textFieldNumber.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u59d3\u540d\uff1a");
            panel1.add(label2);
            label2.setBounds(45, 95, 40, label2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u6027\u522b\uff1a");
            panel1.add(label3);
            label3.setBounds(new Rectangle(new Point(45, 130), label3.getPreferredSize()));

            //---- label4 ----
            label4.setText("\u4e13\u4e1a\uff1a");
            panel1.add(label4);
            label4.setBounds(new Rectangle(new Point(45, 175), label4.getPreferredSize()));

            //---- label5 ----
            label5.setText("\u51fa\u751f\uff1a");
            panel1.add(label5);
            label5.setBounds(new Rectangle(new Point(45, 220), label5.getPreferredSize()));
            panel1.add(textFieldName);
            textFieldName.setBounds(95, 90, 270, textFieldName.getPreferredSize().height);
            panel1.add(textFieldPro);
            textFieldPro.setBounds(95, 170, 270, 30);
            panel1.add(textFieldBorn);
            textFieldBorn.setBounds(95, 220, 270, 30);

            //---- radioButtonMan ----
            radioButtonMan.setText("\u7537");
            panel1.add(radioButtonMan);
            radioButtonMan.setBounds(new Rectangle(new Point(115, 130), radioButtonMan.getPreferredSize()));

            //---- radioButtonWomen ----
            radioButtonWomen.setText("\u5973");
            panel1.add(radioButtonWomen);
            radioButtonWomen.setBounds(new Rectangle(new Point(200, 130), radioButtonWomen.getPreferredSize()));

            //---- buttonFind ----
            buttonFind.setText("\u67e5\u627e");
            buttonFind.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    try {
                        buttonFindMouseClicked(e);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
            panel1.add(buttonFind);
            buttonFind.setBounds(new Rectangle(new Point(385, 20), buttonFind.getPreferredSize()));

            //---- buttonChange ----
            buttonChange.setText("\u786e\u8ba4\u4fee\u6539");
            buttonChange.setEnabled(false);
            buttonChange.addActionListener(e -> {
                try {
                    Buttonchange(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            panel1.add(buttonChange);
            buttonChange.setBounds(new Rectangle(new Point(75, 305), buttonChange.getPreferredSize()));

            //---- buttonBack ----
            buttonBack.setText("\u8fd4\u56de");
            buttonBack.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                }
            });
            panel1.add(buttonBack);
            buttonBack.setBounds(new Rectangle(new Point(330, 305), buttonBack.getPreferredSize()));

            //---- buttonInit ----
            buttonInit.setText("\u4fe1\u606f\u91cd\u7f6e");
            buttonInit.setEnabled(false);
            buttonInit.addActionListener(e -> ButtonTextinit(e));
            panel1.add(buttonInit);
            buttonInit.setBounds(new Rectangle(new Point(195, 305), buttonInit.getPreferredSize()));
            panel1.add(labelError);
            labelError.setBounds(25, 260, 375, 35);

            //---- label6 ----
            label6.setText("\u65b0\u5b66\u53f7\uff1a");
            panel1.add(label6);
            label6.setBounds(new Rectangle(new Point(30, 60), label6.getPreferredSize()));
            panel1.add(textFieldNewnumber);
            textFieldNewnumber.setBounds(95, 55, 270, textFieldNewnumber.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 515, 370);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radioButtonMan);
        buttonGroup1.add(radioButtonWomen);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JLabel label1;
    private JTextField textFieldNumber;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField textFieldName;
    private JTextField textFieldPro;
    private JTextField textFieldBorn;
    private JRadioButton radioButtonMan;
    private JRadioButton radioButtonWomen;
    private JButton buttonFind;
    private JButton buttonChange;
    private JButton buttonBack;
    private JButton buttonInit;
    private JLabel labelError;
    private JLabel label6;
    private JTextField textFieldNewnumber;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
